#ifndef ZIPFLODER_H
#define ZIPFLODER_H
#include "quazip.h"
#include "quazipfile.h"
#include "quazipdir.h"
#include <QDirIterator>


bool createTestArchive(QuaZip &zip, const QString &zipName,
                       const QStringList &fileNames,
                       QTextCodec *codec,
                       const QString &dir)
{

    if (codec != NULL) {
        zip.setFileNameCodec(codec);
    }
    if (!zip.open(QuaZip::mdCreate)) {
        qWarning("Couldn't open %s", zipName.toUtf8().constData());
        return false;
    }
    int i = 0;
    QDateTime dt1;
    foreach (QString fileName, fileNames) {
        QuaZipFile zipFile(&zip);
        QString filePath = QDir(dir).filePath(fileName);
        QFileInfo fileInfo(filePath);
        QuaZipNewInfo newInfo(fileName, filePath);
        if (i == 0)
            newInfo.dateTime = newInfo.dateTime.addSecs(-60);
        else if (i == 1)
            dt1 = newInfo.dateTime;
        else if (i == 2)
            newInfo.dateTime = dt1;
        if (!zipFile.open(QIODevice::WriteOnly,
                newInfo, NULL, 0,
                fileInfo.isDir() ? 0 : 0,0)) {
            qWarning("Couldn't open %s in %s", fileName.toUtf8()
                .constData(), zipName.toUtf8().constData());
            return false;
        }
        if (!fileInfo.isDir()) {
            QFile file(filePath);
            if (!file.open(QIODevice::ReadOnly)) {
                qWarning("Couldn't open %s", filePath.toUtf8()
                    .constData());
                return false;
            }
            while (!file.atEnd()) {
                char buf[4096];
                qint64 l = file.read(buf, 4096);
                if (l <= 0) {
                    qWarning("Couldn't read %s", filePath.toUtf8()
                        .constData());
                    return false;
                }
                if (zipFile.write(buf, l) != l) {
                    qWarning("Couldn't write to %s in %s",
                        filePath.toUtf8().constData(),
                        zipName.toUtf8().constData());
                    return false;
                }
            }
            file.close();
        }
        zipFile.close();
        ++i;
    }
    zip.setComment(QString("This is the test archive"));
    zip.close();
    if (zipName.startsWith("<")) { // something like "<QIODevice pointer>"
        return true;
    } else {
        return QFileInfo(zipName).exists();
    }
}

bool ZipFloder(const QString& floderPath,const QString& zipName)
{
   QString newFloderPath(floderPath);

//   LOG_QDEBUG<<"zippath"<<newFloderPath;
    if(newFloderPath.endsWith('\\')==false&&newFloderPath.endsWith('/')==false)
        newFloderPath.append('/');
    int pathLen = QFileInfo(newFloderPath).path().length()+1;

    QuaZip zip;
    zip.setZipName(zipName);


    QDir srcDir(newFloderPath);
    if (srcDir.exists()) {
        QDirIterator dirIterator(srcDir, QDirIterator::Subdirectories);
        QStringList list;
        while (dirIterator.hasNext()) {
            QString str = dirIterator.next();
            if(QFileInfo(str).isDir())
            {
            }
            else
            {
                list<<str.remove(0,pathLen);
            }
        }
        return createTestArchive(zip,zipName,list,QTextCodec::codecForName("UTF-8"),newFloderPath);
    }
    return false;
}

#endif // ZIPFLODER_H
